import os
import sys
import subprocess

def run_frontend():
    frontend_dir = os.path.join(os.getcwd(), 'frontend')
    print(f"当前工作目录: {os.getcwd()}")
    print(f"前端目录: {frontend_dir}")
    print(f"前端目录是否存在: {os.path.exists(frontend_dir)}")
    
    if not os.path.exists(frontend_dir):
        print("错误：找不到前端目录")
        sys.exit(1)
    
    # 切换到前端目录
    os.chdir(frontend_dir)
    
    # 检查是否需要安装依赖
    if not os.path.exists('node_modules'):
        print("正在安装前端依赖...")
        subprocess.run(['powershell', 'npm', 'install'], check=True)
    
    # 启动前端开发服务器
    print("正在启动前端开发服务器...")
    print("注意：前端服务器启动后将在 http://localhost:8080 上运行")
    subprocess.run(['powershell', 'npm', 'run', 'serve'], check=True)

if __name__ == "__main__":
    try:
        run_frontend()
    except subprocess.CalledProcessError as e:
        print(f"错误：命令执行失败: {e}")
        sys.exit(1)
    except KeyboardInterrupt:
        print("\n用户中断，正在退出...")
        sys.exit(0)
    except Exception as e:
        print(f"错误：{e}")
        sys.exit(1) 